package com.xingdata.jjxc.adapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.iflytek.cloud.InitListener;
import com.xingdata.jjxc.App;
import com.xingdata.jjxc.R;
import com.xingdata.jjxc.activity.CommentListView;
import com.xingdata.jjxc.activity.viewdata.FindAllData;
import com.xingdata.jjxc.enty.FindAllEntity;
import com.xingdata.jjxc.enty.RespEntity;
import com.xingdata.jjxc.http.HttpResponseListener;
import com.xingdata.jjxc.http.HttpUtil;
import com.xingdata.jjxc.utils.BitmapCache;
import com.xingdata.jjxc.utils.BitmapTask;
import com.xingdata.jjxc.utils.BitmapThumbnailTask;
import com.xingdata.jjxc.utils.Consumer;
import com.xingdata.jjxc.utils.ImageTools;
import com.xingdata.jjxc.utils.JUtils;
import com.xingdata.jjxc.utils.MessageCode;
import com.xingdata.jjxc.utils.SDCardTools;
import com.xingdata.jjxc.utils.SystemInfo;
import com.xingdata.jjxc.utils.VideoTools;
import com.xingdata.jjxc.views.CircleImageView;

/**
 * 发现界面适配器，适配器界面有各种监听事件
 * 
 * @author ysc
 * 
 */
public class FindAllAdapter extends BaseAdapter {
	private String TAG = "FindAllAdapter";
	/**找到所有数据private List<FindAllEntity> findAllEntlist;*/
	private List<FindAllEntity> findAllEntlist;
	private LayoutInflater inflater;
	private Context mcontext;
	private FindAllData findAllData;
	private HashMap<String, Bitmap> bitMap = new HashMap<String, Bitmap>();
	private Handler handler;
	/**一个HashMap值private HashMap<String, String> paramsMap = new HashMap<String, String>();*/
	private HashMap<String, String> paramsMap = new HashMap<String, String>();
	private Object downloadObject = new Object();
	private ArrayList<String> downLoadURLs = new ArrayList<String>();
	private ArrayList<ImageView> imageViews;
	private Consumer consumer;

	public FindAllAdapter(View tabview, Context mcontext, List<FindAllEntity> findAllEntlist2,
			ArrayList<ImageView> imageViews, Handler handler, Consumer consumer) {
		this.mcontext = mcontext;
		this.findAllEntlist = findAllEntlist2;
		this.handler = handler;
		this.imageViews = imageViews;
		this.consumer = consumer;
	}

	@Override
	public int getCount() {
		return findAllEntlist == null ? 0 : findAllEntlist.size();
	}

	@Override
	public FindAllEntity getItem(int position) {
		return findAllEntlist.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		/**定义FindAllEntity*/
		final FindAllEntity findAllEntity = findAllEntlist.get(position);
		
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(mcontext).inflate(R.layout.find_all_info, null);

			// 设置字体
			Typeface font = Typeface.createFromAsset(mcontext.getAssets(), JUtils.TypefacePath);
			((TextView) convertView.findViewById(R.id.NickName)).setTypeface(font);
			((TextView) convertView.findViewById(R.id.Time)).setTypeface(font);
			((TextView) convertView.findViewById(R.id.find_all_title_tv)).setTypeface(font);
			((TextView) convertView.findViewById(R.id.ThumbUp)).setTypeface(font);
			((TextView) convertView.findViewById(R.id.Comment)).setTypeface(font);

			holder.find_all_title_tv = (TextView) convertView.findViewById(R.id.find_all_title_tv);
			/** 点赞图标 */
			holder.assistCheckBox = (CheckBox) convertView.findViewById(R.id.assist);
			/** 点赞数量text */
			holder.ThumbUp = (TextView) convertView.findViewById(R.id.ThumbUp);
			/** 举报按钮 */
			holder.report = (ImageView) convertView.findViewById(R.id.report);
			/** 图片预览 */
			holder.find_all_movie_iv = (ImageView) convertView.findViewById(R.id.find_all_movie_iv);

			imageViews.add(holder.find_all_movie_iv);

			/** 留言图标 */
			holder.Comment_iv = (ImageView) convertView.findViewById(R.id.Comment_iv);
			/** 留言数目text */
			holder.Comment = (TextView) convertView.findViewById(R.id.Comment);
			/** 系统时间 */
			holder.Time = (TextView) convertView.findViewById(R.id.Time);
			/** 头像 */
			holder.HeadSculpture = (ImageView) convertView.findViewById(R.id.find_all_head);
			/** 昵称 */
			holder.NickName = (TextView) convertView.findViewById(R.id.NickName);
			/** 视频播放按钮 */
			holder.rdio_paly = (Button) convertView.findViewById(R.id.rdio_paly);
			if (findAllEntity.getDis_id().equals("2")) {
				convertView.findViewById(R.id.video_RelativeLayout).setVisibility(View.INVISIBLE);
			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.find_all_title_tv.setText(findAllEntity.getDis_title());
		holder.ThumbUp.setText(findAllEntity.getSupport());
		holder.Comment.setText(findAllEntity.getComment());
		holder.NickName.setText(findAllEntity.getUser_name());
		holder.Time.setText(findAllEntity.getCreate_time());
		if (BitmapCache.get(findAllEntity.getDis_imageurl()) != null) {
			holder.find_all_movie_iv.setImageBitmap(BitmapCache.get(findAllEntity.getDis_imageurl()));
		} else {
			if (!downLoadURLs.contains(findAllEntity.getDis_imageurl())) {
				downLoadURLs.add(findAllEntity.getDis_imageurl());
				// new getBitMapThread(findAllEntity.getDis_imageurl()).start();

				consumer.add(new BitmapTask(mcontext, findAllEntity.getDis_imageurl(), handler, 1.8f));

			}

		}

		holder.Comment_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = null;
				switch (v.getId()) {
				case R.id.Comment_iv:
					Bundle bundle = new Bundle();
					bundle.putString("dis_id", findAllEntity.getDis_id());
					intent = new Intent(mcontext, CommentListView.class);
					intent.putExtras(bundle);
					mcontext.startActivity(intent);
					break;
				}
			}
		});
		
		/**点赞图标监听事件*/
		holder.assistCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				int inThumbUp = Integer.parseInt(findAllEntity.getSupport());
				if (isChecked) {
					findAllEntity.setSupport((inThumbUp + 1) + "");
					
					paramsMap.put("userid", SystemInfo.getUserInfo().getUserid());
					paramsMap.put("access_token", SystemInfo.getUserInfo().getToken());
					paramsMap.put("dis_id", findAllEntity.getDis_id());
					HttpUtil.getInstance(mcontext).Post(App.ZZD_REQUEST_ASSIST, paramsMap, new HttpResponseListener() {

						@Override
						public void onSuccess(RespEntity respEntity) {

						}

						@Override
						public void onLoading(int vurrent) {

						}

						@Override
						public void onFailure(String result) {

						}
					});
				} else {
					
					findAllEntity.setSupport((inThumbUp - 1) + "");
				}
				notifyDataSetChanged();
			}
		});

		holder.rdio_paly.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				VideoTools.palyVideo(mcontext, findAllEntity.getDis_videourl());
			}
		});

		holder.find_all_movie_iv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				Log.i(TAG, "点击事件");
				VideoTools.palyVideo(mcontext, findAllEntity.getDis_videourl());
			}
		});

		return convertView;
	}

	final class ViewHolder {
		private TextView find_all_title_tv, Date, Time, NickName, ThumbUp, Comment;
		private ImageView report, find_all_movie_iv, HeadSculpture, Comment_iv;
		private CircleImageView circleImageView;
		private CheckBox assistCheckBox;
		private Button rdio_paly;

	}

	// public class TextVeiwOnClickListener implements OnClickListener {
	// int postion;
	//
	// public TextVeiwOnClickListener(int postion) {
	// this.postion = postion;
	// }
	//
	// @Override
	// public void onClick(View v) {
	// // int postion = Integer.parseInt(v.getTag().toString());
	// int inThumbUp = Integer.parseInt(findAllEntlist.get(postion)
	// .getSupport());
	// findAllEntlist.get(postion).setSupport((inThumbUp + 1) + "");
	// notifyDataSetChanged();
	// }
	//
	// }

	// class getBitMapThread extends Thread {
	// String url;
	//
	// public getBitMapThread(String url) {
	// this.url = url;
	// downLoadURLs.add(url);
	// }
	//
	// @Override
	// public void run() {
	// super.run();
	// synchronized (downloadObject) {
	// Bitmap bitmap =ImageTools
	// .getUrlImage(mcontext, url, true);
	// if (bitmap!=null) {
	// bitmap.recycle();
	// bitmap=null;
	// }
	// String imageUrl = SDCardTools
	// .isCacheFileIsExit(SDCardTools.PRODUCTS_IMAGE_NAME)
	// + "/" + ImageTools.changeImageName(url);
	// BitmapCache.put(ImageTools.changeImageName(url),
	// ImageTools.compressImageFromFile(imageUrl,2.0f));
	// Message message = new Message();
	// message.what = MessageCode.MessageCode_RefreshControl;
	// handler.sendMessage(message);
	// }
	//
	// }
	// }

}
